package com.test;

/**
 * 共享数据方法一:
 * 将数据抽象成一个类，并将数据的操作作为这个类的方法
 */
public class demo3 {
    public static void main(String[] args) {
        MyData data = new MyData();
        Runnable add = new AddRunnable(data);
        Runnable dec = new DecRunnable(data);
        for (int i = 0; i < 2; i++) {
            new Thread(add).start();
            new Thread(dec).start();
        }
    }

    static class MyData {
        private int j = 0;

        public synchronized void add() {
            j++;
            System.out.println("线程" + Thread.currentThread().getName() + " j为：" + j);
        }

        public synchronized void dec() {
            j--;
            System.out.println("线程" + Thread.currentThread().getName() + " j为：" + j);
        }

        public int getData() {
            return j;
        }
    }

    static class AddRunnable implements Runnable {
        MyData data;

        public AddRunnable(MyData data) {
            this.data = data;

        }

        public void run() {
            data.add();
        }
    }

    static class DecRunnable implements Runnable {
        MyData data;

        public DecRunnable(MyData data) {
            this.data = data;
        }

        public void run() {
            data.dec();
        }
    }
}

